<?php

namespace App\Console\Commands;

use App\Lib\Search;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class CommandCreateEsAndRedis extends Command
{
    protected $signature = 'CreateEsAndRedis';
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '创建Es索引和redis缓存';

     /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $indexArr = [
            'chat_friend_anchor_user',//交友es
            'chat_friend_user',//交友es
            'kk_user',//用户信息
            'kk_room_none',//房间
            'kk_room_bussiness',//公会房间
            'kk_music',//音乐
            'kk_room_hot',//推荐房间
        ];

        //更新es
        $search = new Search();
        foreach($indexArr as $v){
	        $checkIdex = $search->checkIndexExists($v);
            if(!$checkIdex){
                $params = ['index'=>$v];
                $res = $search->createIndex($params);
                echo '创建ES索引成功-|'.$v." | \r\n";
            } else {
                echo 'ES索引已创建|'.$v." | \r\n";
            }
        }

        // redis
        $res = Redis::SET('CONTENT:CHECK:SWITCH', 1);
        $res = Redis::SET('TRENDS:AUDIT:SWITCH', 1);
        $res = Redis::SET('GRASSLANDSHEEP:GAME:NUM', 100);
        $res = Redis::SET('GRASSLANDSHEEP:TRACKPOOL:TOTAL', 3000);

        $keys = Redis::KEYS('*');
        foreach ($keys as $key) {
            echo $key;
            echo "\r\n";
        }

        echo '创建redis缓存成功'."\r\n";
    }
}
